package com.app.config;


import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Binary Wang
 */
@Configuration
@ConditionalOnClass(WxPayService.class)
@EnableConfigurationProperties({WxPayProperties.class,WxNewPayProperties.class})
@AllArgsConstructor
public class WxPayConfiguration {
  private WxPayProperties properties;
  private WxNewPayProperties wxNewPayProperties;
  @Bean
  @ConditionalOnMissingBean
  public WxPayService wxService() {
    WxPayConfig payConfig = new WxPayConfig();
    payConfig.setAppId(StringUtils.trimToNull(this.properties.getAppId()));
    payConfig.setMchId(StringUtils.trimToNull(this.properties.getMchId()));
    payConfig.setMchKey(StringUtils.trimToNull(this.properties.getMchKey()));
    payConfig.setKeyPath(StringUtils.trimToNull(this.properties.getKeyPath()));
    // 可以指定是否使用沙箱环境
    payConfig.setUseSandboxEnv(false);

    WxPayService wxPayService = new WxPayServiceImpl();


    //绿产
    WxPayConfig newPayConfig = new WxPayConfig();
    newPayConfig.setAppId(StringUtils.trimToNull(this.wxNewPayProperties.getAppId()));
    newPayConfig.setMchId(StringUtils.trimToNull(this.wxNewPayProperties.getMchId()));
    newPayConfig.setMchKey(StringUtils.trimToNull(this.wxNewPayProperties.getMchKey()));
    newPayConfig.setKeyPath(StringUtils.trimToNull(this.wxNewPayProperties.getKeyPath()));
    // 可以指定是否使用沙箱环境
    newPayConfig.setUseSandboxEnv(false);



    wxPayService.addConfig(this.properties.getMchId(),payConfig);
    wxPayService.addConfig(this.wxNewPayProperties.getMchId(),newPayConfig);
    return wxPayService;
  }

}
